<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
     <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float: right" @click="search${modelNameUpperCamel}()" type="primary" size="small">查询结果</el-button>
     </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input style="width: 203px" v-model="listQuery.id" placeholder="关键字"></el-input>
           </el-form-item>
        </el-form>
      </div>
        </el-card>
        <el-card class="operate-container" shadow="never">
            <i class="el-icon-tickets"></i>
            <span>数据列表</span>
            <el-button class="btn-add" @click="handleAdd${modelNameUpperCamel}()" size="mini">添加</el-button>
        </el-card>
        <div class="table-container">
            <el-table ref="${modelNameLowerCamel}Table" :data="list" style="width: 100%" @selection-change="handleSelectionChange" v-loading="listLoading" border>
                <el-table-column type="selection" width="60" align="center"></el-table-column>
                <#list modeListDetails as item>
                    <#if "${item.name}"?contains("logo")||"${item.name}"?contains("Logo")
                    ||"${item.name}"?contains("Photo")||"${item.name}"?contains("photo")
                    ||"${item.name}"?contains("Img")||"${item.name}"?contains("img")
                    ||"${item.name}"?contains("Image")||"${item.name}"?contains("image")
                    ||"${item.name}"?contains("pic")||"${item.name}"?contains("Pic")>
                        <el-table-column label="${item.remark}" align="center">
                            <template slot-scope="scope"><img style="height: 80px" :src="scope.row.${item.name}"></template>
                        </el-table-column>
                    <#elseif "${item.name}"?contains("time")||"${item.name}"?contains("Time")>
                        <el-table-column label="${item.remark}" align="center">
                            <template slot-scope="scope">{{scope.row.${item.name}|formatTime}}</template>
                        </el-table-column>
                        <#else>
                        <el-table-column label="${item.remark}" align="center">
                            <template slot-scope="scope">{{scope.row.${item.name}}}</template>
                        </el-table-column>
                    </#if>
                </#list>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleUpdate${modelNameUpperCamel}(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
      <div class="batch-operate-container">
          <el-select size="small" v-model="operateType" placeholder="批量操作">
              <el-option v-for="item in operates" :key="item.value" :label="item.label"
                         :value="item.value"></el-option>
          </el-select>
          <el-button style="margin-left: 20px" class="search-button" @click="handleBatchOperate()" type="primary"
                     size="small">确定
          </el-button>
      </div>
      <div class="pagination-container">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                         layout="total, sizes,prev, pager, next,jumper" :page-size="listQuery.pageSize"
                         :page-sizes="[10,20,30]" :current-page.sync="listQuery.pageNum" :total="total">
          </el-pagination>
      </div>
  </div>
</template>
<script>
    import {fetch${modelNameUpperCamel}List, delete${modelNameUpperCamel}ById,batchDelete${modelNameUpperCamel}} from '@/api/${modelNameLowerCamel}'
    import {formatDate} from '@/utils/date';

    const defaultListQuery = {
        id: null,
        pageNum: 1,
        pageSize: 10
    };
    export default {
        name: '${modelNameLowerCamel}List',
        data() {
            return {
                operates: [
                    {label: "批量删除", value: "batchDelete"}
                ],
                operateType: null,
                listQuery: Object.assign({}, defaultListQuery),
                list: null,
                total: null,
                listLoading: true,
            }
        },
        created() {
            this.getList();
        },
        filters: {
            formatTime(time) {
                if (time == null || time === '') {
                    return 'N/A';
                }
                let date = new Date(time);
                return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
            }
        },
        methods: {
            getList() {
                this.listLoading = true;
                fetch${modelNameUpperCamel}List(this.listQuery).then(response => {
                    this.listLoading = false;
                    this.list = response.data.records;
                    this.total = response.data.total;
                });
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleUpdate${modelNameUpperCamel}(index, row) {
                this.$router.push({path: '/update${modelNameUpperCamel}', query: {id: row.id}})
            },
            handleAdd${modelNameUpperCamel}() {
                this.$router.push({path: '/add${modelNameUpperCamel}'})
            },
            handleDelete(index, row) {
                this.$confirm('是否要删除该品牌', '提示', {
                    confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
                }).then(() => {
                    delete${modelNameUpperCamel}ById(row.id).then(response => {
                        this.$message({message: '删除成功', type: 'success', duration: 1000});
                        this.getList();
                    });
                });
            },
            handleSizeChange(val) {
                this.listQuery.pageNum = 1;
                this.listQuery.pageSize = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.listQuery.pageNum = val;
                this.getList();
            },
            search${modelNameUpperCamel}() {
                this.listQuery.pageNum = 1;
                this.getList();
            },
            handleBatchOperate() {
                console.log(this.multipleSelection);
                if (this.multipleSelection < 1) {
                    this.$message({message: '请选择一条记录', type: 'warning', duration: 1000});
                    return;
                }
                if (this.operateType === 'batchDelete') {
                    let ids = [];
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        ids.push(this.multipleSelection[i].id);
                    }
                    let data = new URLSearchParams();
                    data.append("ids", ids);
                    batchDelete${modelNameUpperCamel}(data).then(response => {
                        this.getList();
                        this.$message({message: '批量删除成功', type: 'success', duration: 1000});
                    });
                } else {
                    this.$message({message: '请选择批量操作类型', type: 'warning', duration: 1000});
                    return;
                }
            }
        }
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>


